﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Work.BL.Hibernate.Managers;
using Work.BL.Managers;
using Work.BL.POCOs;
using Work.UI.Web.MVC.Code.Common;
using Work.UI.Web.MVC.Models.Registration;

namespace Work.UI.Web.MVC.Controllers
{
    public class RegistrationController : BaseController
    {   
        #region Fields

        private const string USER = "User";

        private readonly IRegionManager _regionManager;

        private readonly IUserManager _userManager;
        private IRoleManager _roleManager;
        private readonly IProfileEmployeeManager _employeeManager;
        private readonly IProfileEmployerManager _employerManager;

        private static UserPOCO _userPoco;
        private static ProfileEmployeePOCO _employeePoco;
        private static ProfileEmployerPOCO _employerPoco;

        private static bool _uniquenessLogin = false;
        private static bool _uniquenessEmail = false;

        #endregion

        #region Constructors

        public RegistrationController(IUserManager userManager, IProfileEmployeeManager employeeManager,
            IRegionManager regionManager, IProfileEmployerManager employerManager, IRoleManager roleManager)
        {
            _regionManager = regionManager;
            _userManager = userManager;
            _employeeManager = employeeManager;
            _employerManager = employerManager;
            _roleManager = roleManager;
        }

        #endregion
        
        #region Web Methods for Employee

        [HttpGet]
        public ActionResult EmployeeStep1()
        {
            return View(new UserModel());
        }

        [HttpPost]
        public ActionResult EmployeeStep1(UserModel userModel)
        {
            if (ModelState.IsValid && _uniquenessLogin && _uniquenessEmail && 
                !_userManager.UniqueUser(userModel.Name) && !_userManager.UniqueEmail(userModel.Email))
            {
                //ModelState.AddModelError("", "");
                RegistrationUserValid(userModel);
                return RedirectToAction("EmployeeStep2");
            }
            return View(new UserModel());
            
        }

        [HttpGet]
        public ActionResult EmployeeStep2()
        {
            if(_userPoco == null)
            {
                return RedirectToAction("EmployeeStep1");
            }
            ViewBag.Region = _regionManager.GetRegions();
            return View(new EmployeeModel());
        }

        [HttpPost]
        public ActionResult EmployeeStep2(EmployeeModel employeeModel)
        {
            if (ModelState.IsValid)
            {
                RegistrationEmployeeValid(employeeModel);
                RegistrationEmployee();
                return RedirectToAction("Employee", "Home");
            }
            ViewBag.Region = _regionManager.GetRegions();
            return View(new EmployeeModel());
        }

        #endregion

        #region Web Methods for Employer

        [HttpGet]
        public ActionResult EmployerStep1()
        {
            return View(new UserModel());
        }

        [HttpPost]
        public ActionResult EmployerStep1(UserModel userModel)
        {
            if (ModelState.IsValid && _uniquenessLogin && _uniquenessEmail)
            {
                RegistrationUserValid(userModel);
                return RedirectToAction("EmployerStep2");
            }
            return View(new UserModel());
        }

        [HttpGet]
        public ActionResult EmployerStep2()
        {
            if (_userPoco == null)
            {
                return RedirectToAction("EmployerStep1");
            }
            ViewBag.Region = _regionManager.GetRegions();
            return View(new EmployerModel());
        }

        [HttpPost]
        public ActionResult EmployerStep2(EmployerModel employerModel)
        {
            if (ModelState.IsValid)
            {
                RegistrationEmployerValid(employerModel);
                RegistrationEmployer();
                return RedirectToAction("Employer", "Home");
            }
            ViewBag.Region = _regionManager.GetRegions();
            return View(new EmployerModel());
        }

        #endregion

        #region Web Methods Uniqueness


        public ActionResult UnLogin(string value)
        {
            if (_userManager.UniqueUser(value))
            {
                _uniquenessLogin = false;
                return JavaScript("true");
            }
            _uniquenessLogin = true;
            return JavaScript("false");

        }

        public ActionResult UnEmail(string value)
        {
            if (_userManager.UniqueEmail(value))
            {
                _uniquenessEmail = false;
                return JavaScript("true");
            }
            _uniquenessEmail = true;
            return JavaScript("false");

        }

        #endregion

        #region Helpers

        private void RegistrationEmployee()
        {
            _userPoco = _userManager.Insert(_userPoco);
            int id = _userPoco.Id;
            _employeePoco.IdUser = id;
            _employeeManager.Insert(_employeePoco);
        }

        private void RegistrationEmployer()
        {
            _userPoco = _userManager.Insert(_userPoco);
            int id = _userPoco.Id;
            _employerPoco.IdUser = id;
            _employerManager.Insert(_employerPoco);
        }

        private void RegistrationEmployeeValid(EmployeeModel employeeModel)
        {
            _employeePoco = new ProfileEmployeePOCO();
            _employeePoco.IdRegion = employeeModel.IdRegion;
            _employeePoco.Birthday = employeeModel.Birthday;
            _employeePoco.Name = employeeModel.Name;
            _employeePoco.Surname = employeeModel.Surname;
            _employeePoco.Phone = employeeModel.Phone;
            _employeePoco.Gender = employeeModel.Gender;
        }

        private void RegistrationEmployerValid(EmployerModel employerModel)
        {
            _employerPoco = new ProfileEmployerPOCO();
            _employerPoco.IdRegion = employerModel.IdRegion;
            _employerPoco.Phone = employerModel.Phone;
            _employerPoco.Company = employerModel.Company;
            _employerPoco.Type = employerModel.Type;
        }

        private void RegistrationUserValid(UserModel userModel)
        {
            _userPoco = new UserPOCO();
            _userPoco.Name = userModel.Name;
            _userPoco.Email = userModel.Email;
            _userPoco.Password = userModel.Password;
            RolePOCO rolePoco = _roleManager.GetRoleByName(USER);
            _userPoco.Roles = new List<RolePOCO>();
            _userPoco.Roles.Add(rolePoco);
        }

        #endregion

    }
}
